 /**
 * Bakalarska prace - Inteligentni budik
 * @package sleepCatcher
 * @file FormAlarms.cpp
 * @author Jan Pesava - xpesav00
 * @email xpesav00@stud.fit.vutbr.cz
 * @date 24. 12. 2011
 */

#include <FSysSystemTime.h>

#include "SCDatabase.h"
#include "SCFormManager.h"
#include "models/SCAlarm.h"
#include "SCTranslator.h"

using namespace Osp::System;

SCAlarm::SCAlarm()
{
	this->id = SCAlarm::NEW_OBJECT;
	this->pAlarmRepeat = new SCAlarmRepeat();
	this->SetDefaultValues();
}

SCAlarm::SCAlarm(int id)
{
	this->id = id;
	this->Load();
}


SCAlarm::~SCAlarm()
{
	delete this->pAlarmRepeat;
}


DateTime
SCAlarm::GetTime()
{
	return this->time;
}


void
SCAlarm::SetTime(DateTime time)
{
	this->time = time;
}


SCAlarmRepeat*
SCAlarm::GetAlarmRepeat()
{
	return this->pAlarmRepeat;
}


void
SCAlarm::SetAlarmRepeat(SCAlarmRepeat *pAlarmRepeat)
{
	this->DeallocAlarmRepeat(this->pAlarmRepeat);
	this->pAlarmRepeat = pAlarmRepeat;
}


String
SCAlarm::GetName()
{
	return this->name;
}


void
SCAlarm::SetName(String name)
{
	this->name = name;
}


String
SCAlarm::GetAwakeSound()
{
	return this->awakeSound;
}

void
SCAlarm::SetAwakeSound(String sound)
{
	this->awakeSound = sound;
}

int
SCAlarm::GetSoundLevel()
{
	return this->soundLevel;
}

void
SCAlarm::SetSoundLevel(int level)
{
	this->soundLevel = level;
}

int
SCAlarm::GetAwakeType()
{
	return this->awakeType;
}

void
SCAlarm::SetAwakeType(int type)
{
	this->awakeType = type;
}

bool
SCAlarm::Load()
{
	SCDatabase::LoadAlarm(this);
	return true;
}

bool
SCAlarm::Save()
{
	SCDatabase::SaveAlarmRepeat(this->GetAlarmRepeat());
	SCDatabase::SaveAlarm(this);
	return true;
}


bool
SCAlarm::GetActive()
{
	return this->active;
}
void
SCAlarm::SetActive(bool active)
{
	this->active = active;
}

bool
SCAlarm::IsActive()
{
	return this->active;
}

bool
SCAlarm::IsActiveDay(int day)
{
	return this->pAlarmRepeat->IsActiveDay(day);
}
void
SCAlarm::Ring()
{
	ArrayList* pArg = new ArrayList();
	pArg->Add(*this);

	SCFormManager::SwitchToForm(SCFormManager::IDF_ALARM_RING, false, pArg, SCFormManager::NO_SLIDE);

	pArg->RemoveAll();
	delete pArg;
}

void
SCAlarm::DeallocAlarmRepeat(SCAlarmRepeat* pAlarmRepeat)
{
	if(pAlarmRepeat != NULL)
		delete pAlarmRepeat;
}


SCAlarm*
SCAlarm::GetFirstActive()
{
	return SCDatabase::GetFirstActiveAlarm();
}

ArrayList*
SCAlarm::GetAllAlarms()
{
	ArrayList *pAlarmsList;
	pAlarmsList = SCDatabase::GetAllAlarms();

	return pAlarmsList;
}

SCAlarm*
SCAlarm::GetAlarm(int id)
{
	SCAlarm* pAlarm;

	if(id == 0)
	{
		pAlarm = new SCAlarm();
	} else {
		pAlarm = SCDatabase::GetAlarm(id);
	}

	return pAlarm;
}

void
SCAlarm::Delete(int id)
{
	SCDatabase::DeleteAlarm(id);
}

void
SCAlarm::SetDefaultValues()
{
	DateTime dateTime;
	SystemTime::GetCurrentTime(WALL_TIME, dateTime);

	this->SetTime(dateTime);
	this->SetAwakeType(AWAKE_TYPE_SOUND);
	this->SetName(SCTranslator::GetText("IDS_ALARMSNAME_DEFAULT"));
	this->SetAwakeSound("good_morning.mp3");
	this->SetSoundLevel(SOUND_LEVEL_DEFAULT);
	this->SetActive(true);
	this->pAlarmRepeat->SetDefaultValues();
}

String
SCAlarm::GetAwakeTypeStr()
{
	int awakeType = this->GetAwakeType();

	if(awakeType == AWAKE_TYPE_SOUND)
	{
		return SCTranslator::GetText("IDS_ALARMSTYPE_SOUND");
	} else if(awakeType == AWAKE_TYPE_VIBRATION)
	{
		return SCTranslator::GetText("IDS_ALARMSTYPE_VIBRATION");
	} else if(awakeType == AWAKE_TYPE_SOUND_VIBRATION)
	{
		return SCTranslator::GetText("IDS_ALARMSTYPE_SOUNDVIBRATION");
	} else {
		return "unknown";
	}
}
